
{extend name='public/window' /}

{block name="content"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form">
                {if isset($info.id)}
                <input type="hidden" name="id" value="{$info.id}">
                {/if}
                <input type="hidden" name="__token__" id="token" value="{:token()}"/>
                <div class="layui-form-item">
                    <label class="layui-form-label">商户类型</label>
                    <div class="layui-input-inline">
                        <select name="merchant_type"  id="type_select"   lay-filter="merchant_type">
                            <option value="1">常驻</option>
                            <option value="2">临户</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item merch">
                    <label class="layui-form-label">商户</label>
                    <div class="layui-input-inline">
                        <select name="merchant_id"  lay-search="" id="merchant_id"   lay-filter="merchant_id">
                            <option value="" >请选择</option>
                            {volist name="merchant_list" id="t"}
                            <option value="{$t.id}" >{$t.name} - {$t.code}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item shinfo" style="display: none">
                    <label class="layui-form-label">商户姓名 *</label>
                    <div class="layui-input-block">
                        <input type="text" name="merch_username"   lay-filter="username" autocomplete="off" placeholder="请输入商户姓名" class="layui-input" >
                    </div>
                </div>
                <div class="layui-form-item shinfo" style="display: none">
                    <label class="layui-form-label">商户手机号码 *</label>
                    <div class="layui-input-block">
                        <input type="text" name="merch_mobile"  lay-filter="mobile" autocomplete="off" placeholder="请输入商户手机号码" class="layui-input" >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">所属部门</label>
                    <div class="layui-input-inline">
                        <select name="department_id" lay-search=""  lay-verify="required">
                            <option value="" >请选择</option>
                            {volist name="department_list" id="t"}
                            <option value="{$t.id}" >{$t.name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">缴费金额 *</label>
                    <div class="layui-input-block">
                        <input type="number" name="amount"  lay-verify="required" lay-filter="amount" autocomplete="off" placeholder="请输入缴费金额" class="layui-input" id="amount">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">缴费类型</label>
                    <div class="layui-input-inline">
                        <select name="cost_type_id" id="cost_type_list" lay-search=""  lay-verify="required" lay-filter="cost_type_list"  lay-verify="required">
                            <option value="0" >请选择</option>
                            {volist name="cost_type_list" id="t"}
                            <option value="{$t.id}" >{$t.cost_name}</option>
                            {/volist}
                        </select>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">收款明细：</label>
                    <div class="layui-input-block">
                        <table class="layui-hide" id="student_table" lay-filter="student_table"></table>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">备注 </label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容"  class="layui-textarea" name="remark"></textarea>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin " style="width: 100%">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0px;text-align: center;">
                            <button class="layui-btn" lay-submit="" lay-filter="add">{:__('Submit')}</button>
                            <button type="reset" class="layui-btn layui-btn-primary">{:__('Reset')}</button>
                        </div>
                    </div>
                </div>

            </form>
        </div>
    </div>
</div>
<script>
    layui.use(['element', 'form', 'table', 'jquery','lucky','laydate','xmSelect'], function () {
        var element = layui.element;
        var table = layui.table;
        var form = layui.form;
        var laydate = layui.laydate;
        var $ = layui.jquery;
        var lucky=layui.lucky;
        form.on('select(merchant_type)', function(data){
            var  type = data.value;
            if(type==1){
                $(".shinfo").css('display','none');
                $(".merch").css('display','block');
            }else{
                $(".shinfo").css('display','block');
                $(".merch").css('display','none');
            }
        });
        var costType = [];
        var student_table = table.render({
            id:'student_table',
            elem:'#student_table',
            cols: [[
                {field: 'cost_name', title: '收费名称'},
                {field: 'type', title: '收费类型', templet: function (item) {
                        var type = item.type
                        if (type == 1) {
                            return '押金'
                        } else if (type == 2) {
                            return '费用'
                        } else if (type == 3) {
                            return '证件'
                        } else {
                            return '未知类型'
                        }
                    }},
                {title: '单价', field: 'unitprice'},
                {title: '金额', field: 'z_money'},
            ]],
            data: costType
        });
        // cost_type_list
        form.on('select(cost_type_list)', function(data){
            student_table.reload({data:[]});
            if (!data.value){
                return false
            }
            var  amount = $("#amount").val();
            if (!amount){
                layer.msg("请先填写缴费金额",{icon:15,time:1000,shade:0.3});
                return false;
            }
            var datas = {} ;
            datas.amount      = amount ;   //
            datas.cost_type   = data.value ;   // 缴费类型
            costtype(datas) ;
            return false;
        });

        $("#amount").on("input",function(e){
            console.log('监听中.....')
            student_table.reload({data:[]});
            var amount = e.delegateTarget.value;
            var cost_type = $("#cost_type_list").val();
            if (!cost_type){
                return false;
            }
            var datas = {} ;
            datas.amount      = amount ;   //  购买数量
            datas.cost_type   = cost_type ;   // 缴费类型
            console.log('datas',datas)
            costtype(datas) ;
            return false;
        });

        function costtype(datas){
            $.ajax({
                url:"{:url('getCostType')}",
                type:"post",
                data: datas,
                dataType:"json",
                success:function(data){
                    var param = data.data
                    student_table.reload({data:param.data});
                }
            });
        }
        //表单赋值
        var info = {:json_encode($info)};
        form.val("form",info);
        form.on('submit(add)', function (data) {
            var datas  = layui.table.cache["student_table"]
            data.field.type = 1 // 添加type
            data.field.is_admin_create = 1 // 添加type
            data.field.pay_status = 0 // 支付状态
            data.field.datas = datas // 添加type
            lucky.FormSubmit("{:url('add')}",data.field,true);
            return false;
        });
    })
</script>

{/block}

